# @Time    : 2024/12/31 10:42
# @Author  : Zhang Yihao

def open_r():
    """
    读取文件
    """
    file=open('file2.txt',mode='r',encoding='utf-8')
    text=file.read()
    print(text)
    file.close()
def open_rw():
    """
    读取文件
    """
    file=open('file2.txt',mode='r+',encoding='utf-8') #文件不存在就会创建，存在就会清空
    text=file.read()
    print(text)
    file.write(' hello world123')
    file.close()
def open_w():
    """
    读取文件
    """
    file=open('file3.txt',mode='w',encoding='utf-8') #文件不存在就会创建，存在就会清空
    file.write(' hello world 123')
    file.close()

def open_a():
    """
    每次写加到末尾
    """
    file=open('file3.txt',mode='r+',encoding='utf-8')
    file.write(' how r+') #模式为r+时，写操作会覆盖文件内容，在开头
    file.close()

def use_readline():
    file=open('file2.txt',encoding='utf-8')
    while True:
        # 读取一行
        text=file.readline()
        # 判断是否读取完毕
        if not text:
            break

        print(text)
    file.close()

if __name__ == '__main__':
    # open_r()
    # open_rw()
    # open_w()
    # open_a()
    use_readline()